package com.emergency.evaluate.service.impl;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.evaluate.dao.ExtractDao;
import com.emergency.evaluate.dao.ManualDao;
import com.emergency.evaluate.dao.ReportDao;
import com.emergency.evaluate.dao.ScoreDao;
import com.emergency.evaluate.domain.*;
import com.emergency.evaluate.service.ManualService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Administrator on 2020/3/16.
 */
@Service
@Transactional
public class ManualServiceImpl implements ManualService{
    //输出日志
    private final static Logger logger = LoggerFactory.getLogger(ManualServiceImpl.class);
    @Autowired
    private ManualDao manualDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private ExtractDao extractDao;
    @Autowired
    private ReportDao reportDao;



    //根据想定编号查询评分表
    @Override
    public List<Manual> findAllByScenar(Long scenarId){
        List<Manual> manuals=manualDao.findAllByScenar(scenarId);
        logger.info("根据想定编号查询评分表");
        return manuals;
    }

    //保存评分表
    @Override
    public Manual save(Manual manual){
        return manualDao.save (manual);
    }

    //删除评分表
    @Override
    public void delete(Long id){
        //先删除score表里的数据
        scoreDao.deleteAllByManualId (id);
        //在把manual删除
        manualDao.deleteById (id) ;
        //把manual表对应的report也删除
        reportDao.deleteByManual ( id );
}
   //添加人工评分表
    @Override
    @Transactional
    public List<Manual> add(Manual manual) {

        //manual没有ID则自动就增加ID
        if(CommonUtils.isNullOrEmpty(manual.getId())){
            manual.setId(idWorker.nextId());
            manual.setCreateTime ( new Date());
        }

        // 如果没有选择打分方式
        if(manual.getMethod ()==null){
            manual.setMethod (1);//默认打分方式为人工评估
        }
        manualDao.save(manual);
        //先删除manual 相关的分数设置
        scoreDao.deleteByManual(manual.getId());
        double pscore = 0;
        //人工评分，一定有评分表的分数信息
            //将人工打的分数一一对应录入分数表中，在与总的评分表绑定
            List <Score> scores = manual.getScores ();
            for (Score score : scores) {
                if (CommonUtils.isNullOrEmpty ( score.getId () )) {
                    score.setId ( idWorker.nextId () );
                    score.setCreateTime ( new Date () );
                }
                score.setExtract( extractDao.findAllById ( score.getExtract ().getId () ) );
                if (!CommonUtils.isNullOrEmpty ( scoreDao.findAllById ( score.getId () ) )) {
                    Score score1 = scoreDao.findAllById ( score.getId () );
                    score.setCreateTime ( score1.getCreateTime () );
                    if (CommonUtils.isNullOrEmpty ( score.getPname () )) {
                        score.setPname ( score1.getPname () );
                    }
                    if (CommonUtils.isNullOrEmpty ( score1.getExtract () )) {
                        score.setExtract ( score1.getExtract () );
                    }
                    if (CommonUtils.isNullOrEmpty ( score1.getManual () )) {
                        score.setManual ( score1.getManual () );
                    }
                    score.setModifyTime ( new Date () );}
                    //设置分数等级
                    if (score.getScore () < 100 && (score.getScore () >= 90)) {
                        score.setLevel ( "A" );
                    } else if (score.getScore () < 90 && (score.getScore () >= 80)) {
                        score.setLevel ( "B" );
                    } else if (score.getScore () < 80 && (score.getScore () >= 70)) {
                        score.setLevel ( "C" );
                    } else if (score.getScore () < 70 && (score.getScore () >= 60)) {
                        score.setLevel ( "D" );
                    } else if (score.getScore () < 60) {
                        score.setLevel ( "E" );
                    }
                    score.setManual ( manual );
                    pscore = pscore+score.getScore () * score.getExtract ().getWeight (); //总分等于分数*权重
                    scoreDao.save ( score );
            }
            manual.setPscore ( pscore );
        manualDao.save ( manual );
        return manualDao.findallById ( manual.getId () );

    }

    @Override
    public Manual get(Long id) {
        Manual manual =  manualDao.findAllById (id);
        if(CommonUtils.isNullOrEmpty(manual)){
            logger.info("评分表不存在！");
            return manual;
        }
        manual.setScores(scoreDao.findAllByManual(id));
        return manual;
    }

}

